<template>
	<view class="container">
		<form @submit="formSubmit" class="flex-column-xy-center">
			<button class="avatar-wrapper" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
				<image mode="scaleToFill" class="avatar" src="/static/images/default-avatar.png" v-if="!form.avatar"></image>
				<image mode="scaleToFill" class="avatar" :src="form.avatar | formatFile" v-else></image>
			</button>
			<view class="tip">点击上传头像</view>
			<view class="nickname-title">昵称</view>
			<view class="nickname-input flex-y-center">
				<input type="nickname" placeholder="请输入昵称" name="nickname">
			</view>

			<button class="submit-btn" form-type="submit">保存</button>

		</form>
	</view>
</template>

<script>
	import {
		wechatAuth,
		wechatAuthPhone
	} from '@/api/user.js'
	import {
		uploadImage,
		uploadFile
	} from '@/utils/upload.js'
	export default {
		data() {
			return {
				form: {
					avatar: '',
					nickName: ''
				},
				isAuth: false
			}
		},
		methods: {
			formSubmit(e) {
				this.form.nickName = e.detail.value.nickname || ''
				// if (!this.form.avatar) {
				// 	return this.toast('头像不能为空')
				// }
				if (!this.form.nickName) {
					return this.toast('昵称不能为空')
				}
				uni.login({
					success: (data) => {
						this.form.code = data.code
						wechatAuth(this.form).then(res => {
							this.toast("授权成功");
							this.$store.commit('user/SET_TOKEN', res.data.token)
							this.$store.commit('user/SET_LOGIN', true);
							this.$store.commit('user/SET_USERID', res.data.userId);
							setTimeout(() => {
								uni.switchTab({
									url: '/pages/index/index'
								})
							},  500)
						}).catch(err => {
							console.log(err)
						})
					}
				})
			},
			getPhoneNumber(e) {
				wechatAuthPhone({
					encryptedData: e.detail.encryptedData,
					iv: e.detail.iv
				}).then(res => {
					uni.reLaunch({
						url: '/pages/index/index'
					})
				})
			},
			onChooseAvatar({
				target
			}) {
				uploadFile(target.avatarUrl).then(res => {
					this.form.avatar = res.fileName
				})
				
			}
		}
	}
</script>

<style lang="scss">
	.container {
		display: flex;
		flex-direction: column;
		align-items: center;

		.avatar {
			width: 180rpx;
			height: 180rpx;
			border-radius: 50%;
		}

		.avatar-wrapper {
			padding: 0;
			border-radius: 8px;
			background-color: #fff;
			width: 180rpx;
			height: 180rpx;
			margin-top: 150rpx;
		}

		.avatar-wrapper::after {
			border: 0;
		}


		.tip {
			margin-top: 20rpx;
			font-size: 30rpx;
			font-weight: 500;
			color: #808080;
			text-align: center;
		}

		.nickname-title {
			font-size: 30rpx;
			font-weight: bold;
			color: #333333;
			margin: 100rpx 0 30rpx;
			align-self: flex-start;
		}

		.nickname-input {
			width: 630rpx;
			height: 88rpx;
			background: #F8F8F8;
			border-radius: 12rpx;
			padding: 30rpx;
			margin-bottom: 100rpx;

			input {
				font-size: 32rpx;
				font-weight: 400;
				color: #757575;
				margin: 0 28rpx 0rpx;
				font-size: 32rpx;
				font-weight: 400;
				color: #757575;
			}
		}


	}
</style>